﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class monthTicket : System.Web.UI.Page
{
    //inject EmployeeBLL
    //private IMonthCardBLL monthBLL = Factory<IMonthCardBLL>.CreateInstance();
    MonthCard monthBLL = null;
    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(monthTicket).Name);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadToRepeater();
        }
    }

    private void LoadToRepeater()
    {
        try
        {
            List<MonthCard> list = monthBLL.GetAll();
            if (list != null)
            {
                rptMonth.DataSource = list;
                rptMonth.DataBind();

                //hide to msg labels
                lblMsg.Visible = false;
                lblMsg.Visible = false;
            }
            else
            {
                rptMonth.Visible = false;
                lblMsg.Text = "Không có thẻ tháng nào.";
                lblMsg.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
            lblMsg.Visible = true;
        }
    }

    /// <summary>
    /// Delete
    /// </summary>
    /// <param name="id"></param>
    private void Delete(Guid id)
    {
        try
        {
            if (monthBLL.Delete(id))
                LoadToRepeater();
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            lblMsg.Visible = true;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    private void Active(string command)
    {
        try
        {
            //init BO
            MonthCard card = new MonthCard();
            card.ID = new Guid(command.Split(';')[1]);
            card.Active = Boolean.Parse(command.Split(';')[0]);

            if (monthBLL.ActiveAction(card))
                LoadToRepeater();

        }
        catch (Exception ex)
        {
            lblMsg.Visible = true;
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// When press icons
    /// </summary>
    /// <param name="strCommandName"></param>
    /// <param name="strArgument"></param>
    private void Action(string strCommandName, string strArgument)
    {
        switch (strCommandName.ToUpper())
        {
            //press delete button
            case "DELETE":
                Delete(new Guid(strArgument));
                break;
            //press edit icon
            case "EDIT":
                this.Context.Items["id"] = strArgument;
                this.Context.Items["command"] = "EDIT";
                Server.Transfer(Constants.MONTH_TICKET_ACTION);
                break;
            //press extend icon
            case "EXTEND":
                this.Context.Items["command"] = "EXTEND";
                this.Context.Items["id"] = strArgument;
                Server.Transfer(Constants.MONTH_TICKET_ACTION);
                break;
            //press active icon
            case "ACTIVE":
                Active(strArgument);
                break;
            default:
                break;
        }
    }

    private void Search()
    {
        //MonthCard month = new MonthCard();
        //month.PlateNumber = txtPlat.Text.Trim();
        ////month.FullName = txtFullname.Text.Trim();
        //try
        //{
        //    List<MonthCard> list = monthBLL.Search(month);

        //    if (list != null)
        //    {
        //        rptMonth.DataSource = list;
        //        rptMonth.DataBind();
        //        rptMonth.Visible = true;
        //        //hide to msg labels
        //        lblMsg.Visible = false;
        //        lblMsg.Visible = false;
        //    }
        //    else
        //    {
        //        rptMonth.Visible = false;
        //        lblMsg.Text = "Không có thẻ tháng nào phù hợp.";
        //        lblMsg.Visible = true;
        //    }
        //}
        //catch (Exception ex)
        //{
        //   lblMsg.Visible = true;
        //    lblMsg.Text = Constants.ERROR_MSG;
        //    if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        //}
       
    }

    protected void rptMonth_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //get command
        string strCommandName = e.CommandName.Trim();
        string strArgument = e.CommandArgument.ToString().Trim();

        Action(strCommandName, strArgument);
    }
    protected void rptMonth_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //get Employee from session
        MonthCard month = (MonthCard)e.Item.DataItem; ;

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //find control

            ImageButton btnActive = (ImageButton)e.Item.FindControl("btnActive");
            
            if (month.Active)
            {
                btnActive.ImageUrl = Constants.ACTIVE_IMAGE;
                btnActive.ToolTip = "Click để vô hiệu hóa";
            }
        }
    }
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        Search();
        
    }
}
